/*
Copyright 2005-2014 CP Fricano - All Rights Reserved

This software and source code is licensed under the Apache
License, Version 2.0. You may not use this software or source
code except in compliance with the License.  You may obtain a
copy of the License at: http://www.apache.org/licenses/LICENSE-2.0.
Use and distributed under this License is on an "as is" basis,
without expressed or implied warranties or conditions of any kind.
See the License for the specific language governing permissions and
limitations.
 */

package com.fricano.java42.weather.api.provider.adapter;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.fricano.java42.base.components.callerid.impl.CallerId;
import com.fricano.java42.base.components.datapointprovider.impl.ProviderInfo;
import com.fricano.java42.base.components.utility.impl.Utility;

public abstract class WeatherProviderInfo extends ProviderInfo {

    protected final Map<Object, List<Map<String, Object>>> map        = new HashMap<>();

    final private static List<Map<String, Object>>         LIST_EMPTY = new ArrayList<>();

    protected WeatherProviderInfo(final Object apiKey) {
        super(apiKey, WeatherProviderInfo.class);
    }

    public BufferedImage getBufferedImage() {
        return (BufferedImage) _dataPointMap.get(BufferedImage.class.getSimpleName());
    }

    public long getDataPointAge(final TimeUnit timeUnits) {
        return Utility.translateDateDifference(getTime(), new Date(), timeUnits);
    }

    public Date getDate() {
        return (Date) _dataPointMap.get(Key.DATE);
    }

    public <T> T getFullLocation() {
        return (T) _dataPointMap.get(Key.LOCATION_FULL);
    }

    public <T> T getObjectByKey(final Object key) {
        final Object object = _dataPointMap.get(key);
        if (isDevelopmentMode()) {
            final String type = object != null ? object.getClass().getSimpleName() : "null";
            CallerId.identifyMessage("WAPI0091D: Object is type " + type + " for key " + key.toString());
        }
        return (T) object;
    }

    public <T> T getObjectByKey(final Key key, final int index) {
        final Object object = _dataPointMap.get(key.shared(index));
        if (isDevelopmentMode()) {
            final String type = object != null ? object.getClass().getSimpleName() : "null";
            CallerId.identifyMessage("WAPI0091D: Object is type " + type + " for key " + key.shared(index).toString());
        }
        return (T) object;
    }

    public Double getLatitude() {
        return (Double) _dataPointMap.get(Key.LOCATION_LATITUDE);
    }

    public Double getLongitude() {
        return (Double) _dataPointMap.get(Key.LOCATION_LONGITUDE);
    }

    public List<Map<String, Object>> getMap(final Key key) {
        List<Map<String, Object>> list = LIST_EMPTY;
        final List<Map<String, Object>> i = map.get(key);
        if (i != null) {
            list = i;
        }
        return list;
    }

    public double getTemperature() {
        double d = 0.0;
        final Object object = _dataPointMap.get(Key.TEMPERATURE);
        if (object != null) {
            try {
                d = Double.parseDouble(object.toString());
            } catch (final Exception e) {
            }
        }
        return d;
    }

    public Date getTime() {
        Date date = null;
        final Object object = _dataPointMap.get(Key.TIME);
        if (object instanceof Date) {
            date = (Date) object;
        }
        return (date);
    }

}
